package com.itheima.redbaby.activity;

import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.itheima.redbaby.BaseActivity;
import com.itheima.redbaby.R;
import com.itheima.redbaby.bean.Category;
import com.itheima.redbaby.engine.CategoryEngine;
import com.itheima.redbaby.factory.DaoAndEngineFactory;

public class CategoryChildActivity extends BaseActivity {
	private int parentid;
	private CategoryChildAdapter adapter;
	private String titlename;
	private ListView lv_category;
	private List<Category> category_list;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.category_child_activity);
		Intent preIntent = getIntent();
		parentid = preIntent.getIntExtra("parentid", -1);
		titlename = preIntent.getStringExtra("parentname");
		
		init();
	}
	
	private void init() {
		TextView titleBack = (TextView) findViewById(R.id.titleBack);
		TextView categoryTitle = (TextView) findViewById(R.id.categoryTitle);
		lv_category = (ListView) findViewById(R.id.categoryList);
		
		categoryTitle.setText(titlename);
		titleBack.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		
		adapter = new CategoryChildAdapter();
		lv_category.setAdapter(adapter);
		lv_category.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Category category = (Category) lv_category.getItemAtPosition(position);
				
				//如果是最后一个分类，进入商品界面
				if("true".equals(category.getIsleafnode())) {
					Intent intent = new Intent(CategoryChildActivity.this, CategoryProductListActivity.class);
					intent.putExtra("cId", category.getId());
					intent.putExtra("cName", category.getName());
					startActivity(intent);
				//不是最后一个分类界面
				} else {
					Intent intent = new Intent(CategoryChildActivity.this, CategoryChildActivity.class);
					intent.putExtra("parentid", category.getId());
					intent.putExtra("parentname", category.getName());
					startActivity(intent);
				}
			}
		});
		
		getdata();
	}
	
	/**
	 * 后台获取数据
	 */
	private void getdata() {
		new MyHttpTask() {
			@Override
			protected Object doInBackground(Void... params) {
				CategoryEngine categoryEngine = DaoAndEngineFactory.getImpl(CategoryEngine.class);
				//获取到分类集合
				List<Category> list = categoryEngine.getCategoryList(parentid);
				return list;
			}
			@Override
			@SuppressWarnings("unchecked")
			protected void onPostExecute(Object result) {
				if(result != null) {
					category_list = (List<Category>) result;
					adapter.notifyDataSetChanged();
				}
			}
		}.executeHttp();
	}

	class CategoryChildAdapter extends BaseAdapter {
		@Override
		public int getCount() {
			if(category_list == null || category_list.size() == 0)
				return 0;
			return category_list.size();
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if(convertView == null) {
				convertView = View.inflate(CategoryChildActivity.this, R.layout.category_child_item, null);
			}
			TextView content = (TextView) convertView.findViewById(R.id.textContent);
			Category category = category_list.get(position);
			content.setText(category.getName());
			return convertView;
		}

		@Override
		public Object getItem(int position) {
			Category category = category_list.get(position);
			return category;
		}
		@Override
		public long getItemId(int position) {
			return position;
		}
	}
}
